//
//  HomeViewController.m
//  智慧银行
//
//  Created by rimi on 15/9/28.
//  Copyright (c) 2015年 weilai. All rights reserved.
//

#import "HomeViewController.h"
#import "NetworkRequest.h"
#import "URL.h"
#import "Parse.h"
#import "UIImageView+WebCache.h"
@import MediaPlayer;

#define DETALIE_COLOR [UIColor colorWithRed:223/255.0 green:216/255.0 blue:204/255.0 alpha:1]
@interface HomeViewController ()
@property (nonatomic, strong) NetworkRequest *homePageRequest;
@property (nonatomic, strong) UIView *detalieView;
@property (nonatomic, strong) UIView *leftMidView;
@property (nonatomic, strong) UIView *rightMidView;
@property (nonatomic, strong) NSArray *convertArray;
@property (nonatomic, strong) MPMoviePlayerViewController *playerVC;
@end

@implementation HomeViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initlizeuserinterface];
}

- (void)initlizeuserinterface
{
    self.view.backgroundColor = BACK_COLOR;
    self.headView.image = [UIImage imageNamed:@"首页页眉"];
    
    UITapGestureRecognizer *headTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickHeadImage)];
    UIImageView * imageHead = [[UIImageView alloc]initWithFrame:CGRectMake(50, 125, 610, 210)];
    imageHead.tag = 1000;
    [self.view addSubview:imageHead];
    imageHead.userInteractionEnabled = YES;
    [imageHead addGestureRecognizer:headTap];
    imageHead.transform = CGAffineTransformMakeRotation(-0.06f);
    
    UITapGestureRecognizer *midLeftTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickMidLeftImage)];
    UIImageView * imageMidLeft = [[UIImageView alloc]initWithFrame:CGRectMake(35, 300, 320, 320)];
    imageMidLeft.tag = 1001;
    [self.view addSubview:imageMidLeft];
    imageMidLeft.userInteractionEnabled = YES;
    [imageMidLeft addGestureRecognizer:midLeftTap];
    imageMidLeft.transform = CGAffineTransformMakeRotation(-0.07f);
    
    UITapGestureRecognizer *midRightTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickMidRightImage)];
    UIImageView * imageMidRight = [[UIImageView alloc]initWithFrame:CGRectMake(340, 320, 320, 320)];
    imageMidRight.tag = 1002;
    [self.view addSubview:imageMidRight];
    imageMidRight.userInteractionEnabled = YES;
    [imageMidRight addGestureRecognizer:midRightTap];
    imageMidRight.transform = CGAffineTransformMakeRotation(0.18f);
    
    UITapGestureRecognizer *lopTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickLopImage)];
    UIImageView * imageLop = [[UIImageView alloc]initWithFrame:CGRectMake(26, 615, 640, 330)];
    [self.view addSubview:imageLop];
    imageLop.userInteractionEnabled = YES;
    [imageLop addGestureRecognizer:lopTap];
    imageLop.tag = 1003;
    imageLop.transform = CGAffineTransformMakeRotation(0.1f);
    
    self.homePageRequest = [[NetworkRequest alloc] init];
    [self.homePageRequest post:kHomePageURL parameters:nil successHandle:^(NSString *responds) {
        
        NSArray *nodeNames = @[@"url", @"index",@"isOpen", @"detailUrl", @"content"];
        Parse *parse = [[Parse alloc] init];
        NSArray *resultArr = [parse parse:responds nodePath:@"////com.chinaxyl.show.pojo.MediaPojo" nodeNames:nodeNames];
        NSLog(@"parse result = %@",resultArr);
        
        _convertArray = [self covertURL:resultArr];
        [self viewsLoadData:_convertArray];
        NSLog(@"=====%@",_convertArray);
        
    } failureHandle:^(NSError *error) {
        
    }];
}
- (NSArray *)covertURL:(NSArray *)orginArray {
    
    NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:1];
    for (NSDictionary *dic in orginArray) {
        
        NSMutableDictionary *resultDic = [NSMutableDictionary dictionaryWithDictionary:dic];
        
        NSMutableString *orginURLStr = [dic[@"url"] mutableCopy];
        NSRange range = [orginURLStr rangeOfString:@"http://127.0.0.1:8081/icbcfile/a/"];
        if (range.length > 0) {
            [orginURLStr replaceCharactersInRange:range withString:kBaseURL];
        }
        [resultDic setObject:orginURLStr forKey:@"url"];
        
        NSMutableString *orginDetailURLStr = [dic[@"detailUrl"] mutableCopy];
        NSRange detailRange = [orginDetailURLStr rangeOfString:@"http://127.0.0.1:8081/icbcfile/a/"];
        if (detailRange.length > 0) {
            [orginDetailURLStr replaceCharactersInRange:detailRange withString:kBaseURL];
        }
        [resultDic setObject:orginDetailURLStr forKey:@"detailUrl"];
        [resultArray addObject:resultDic];
    }
    
    return resultArray;
}

- (void)viewsLoadData:(NSArray *)datas {
    
    for (NSDictionary * obj in datas) {
        NSInteger index = [obj[@"index"] integerValue];
        UIImageView *imageView = (UIImageView *)[self.view viewWithTag:999 + index];
        NSString *urlString = obj[@"url"];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil];
    }
}


- (void)onClickHeadImage
{
    _detalieView = [[UIView alloc]initWithFrame:CGRectMake(13, 985, 668, 0)];
    _detalieView.backgroundColor = DETALIE_COLOR;
    _detalieView.layer.cornerRadius = 7;
    _detalieView.alpha = 0.95;
    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
    close.frame = CGRectMake(610, 5, 50, 40);
    [close setBackgroundImage:[UIImage imageNamed:@"关闭_05"] forState:UIControlStateNormal];
    [_detalieView addSubview:close];
    close.tag = 101;
    [close addTarget:self action:@selector(handleCloseButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_detalieView];
    [UIView animateWithDuration:0.3 animations:^{
        _detalieView.frame = CGRectMake(13, 80, 668, 905);
    }];

    UIImageView *titleView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 75, 550, 160)];
    [_detalieView addSubview:titleView];
    NSString *urlString = _convertArray[0][@"url"];
    [titleView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil];
    
    UITextView *introduce = [[UITextView alloc]initWithFrame:CGRectMake(60, 255, 550, 260)];
    introduce.editable = NO;
    introduce.text = _convertArray[0][@"content"];
    introduce.font = [UIFont systemFontOfSize:17];
    introduce.backgroundColor = [UIColor clearColor];
    [_detalieView addSubview:introduce];
    
}

- (void)onClickMidLeftImage
{
    _leftMidView = [[UIView alloc]initWithFrame:CGRectMake(13, 985, 668, 0)];
    _leftMidView.backgroundColor = DETALIE_COLOR;
    _leftMidView.layer.cornerRadius = 7;
    _leftMidView.alpha = 0.95;
    [self.view addSubview:_leftMidView];
    
    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
    close.frame = CGRectMake(610, 5, 50, 40);
    [close setBackgroundImage:[UIImage imageNamed:@"关闭_05"] forState:UIControlStateNormal];
    
    close.tag = 102;
    [close addTarget:self action:@selector(handleCloseButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [UIView animateWithDuration:0.3 animations:^{
        _leftMidView.frame = CGRectMake(13, 80, 668, 905);
    }];

    UIScrollView *scorllView = [[UIScrollView alloc]initWithFrame:_leftMidView.bounds];
    scorllView.pagingEnabled = NO;
    scorllView.contentSize = CGSizeMake(_leftMidView.bounds.size.width, 2500);
    
    [_leftMidView addSubview:scorllView];
    [_leftMidView addSubview:close];
    UIImageView *headView = [[UIImageView alloc]initWithFrame:CGRectMake(220, 60, 180, 180)];
    [scorllView addSubview:headView];
    NSString *urlString = _convertArray[1][@"url"];
    [headView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil];
    UITextView *detaile = [[UITextView alloc]initWithFrame:CGRectMake(60, 255, 550, 2300)];
    detaile.editable = NO;
    detaile.text = _convertArray[1][@"content"];
    detaile.backgroundColor = [UIColor clearColor];
    detaile.font = [UIFont systemFontOfSize:17];
    [scorllView addSubview:detaile];
    
}

- (void)onClickMidRightImage
{
    _rightMidView = [[UIView alloc]initWithFrame:CGRectMake(13, 985, 668, 0)];
    _rightMidView.backgroundColor = DETALIE_COLOR;
    _rightMidView.layer.cornerRadius = 7;
    _rightMidView.alpha = 0.95;
    [self.view addSubview:_rightMidView];
    
    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
    close.frame = CGRectMake(610, 5, 50, 40);
    [close setBackgroundImage:[UIImage imageNamed:@"关闭_05"] forState:UIControlStateNormal];
    [_rightMidView addSubview:close];
    close.tag = 103;
    [close addTarget:self action:@selector(handleCloseButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [UIView animateWithDuration:0.3 animations:^{
        _rightMidView.frame = CGRectMake(13, 80, 668, 905);
    }];
    
    UIImageView *headView = [[UIImageView alloc]initWithFrame:CGRectMake(220, 60, 180, 180)];
    [_rightMidView addSubview:headView];
    NSString *urlString = _convertArray[2][@"url"];
    [headView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil];
    
    UITextView *introduce = [[UITextView alloc]initWithFrame:CGRectMake(60, 255, 550, 460)];
    introduce.editable = NO;
    introduce.text = _convertArray[2][@"content"];
    introduce.font = [UIFont systemFontOfSize:17];
    introduce.backgroundColor = [UIColor clearColor];
    [_rightMidView addSubview:introduce];


}

- (void)onClickLopImage
{
    //0 注册通知(当视屏播放完毕或用户结束时发送通知)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMovieEvent:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    //设置url
    NSURL *url = [NSURL URLWithString:_convertArray[3][@"detailUrl"]];
    //创建playerVC
    self.playerVC = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
    //推送
    [self presentMoviePlayerViewControllerAnimated:self.playerVC];
}

- (void)handleCloseButtonEvent:(UIButton *)sender
{
    if (sender.tag == 101) {
        [UIView animateWithDuration:0.3 animations:^{
            _detalieView.frame = CGRectMake(13, 985, 668, 0);
        } completion:^(BOOL finished) {
            [_detalieView removeFromSuperview];
        }];
    } else if (sender.tag == 102){
        [UIView animateWithDuration:0.3 animations:^{
            _leftMidView.frame = CGRectMake(13, 985, 668, 0);
        } completion:^(BOOL finished) {
            [_leftMidView removeFromSuperview];
        }];
    } else {
        [UIView animateWithDuration:0.3 animations:^{
            _rightMidView.frame = CGRectMake(13, 985, 668, 0);
        } completion:^(BOOL finished) {
            [_rightMidView removeFromSuperview];
        }];

    }
}

- (void)handleMovieEvent:(NSNotification *) not
{
    NSLog(@"放完了， 大兄弟");
}

@end
